package com.app.mingluxing.chat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.SearchContract;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

/**
 * 查找好友页面
 * Created by Aaron on 16/1/18.
 */
public class SearchContractActivity extends BaseActivity {

    private EditText etphone;
    private TextView tvnickname;
    private TextView tvtel;
    private ImageView iv_pic;
    private TextView tv_add;

    private String invitePhoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_contract);

        initWidget();

    }

    long lastTime = 0;

    private void initWidget() {

        this.tvtel = (TextView) findViewById(R.id.tv_tel);
        this.tvnickname = (TextView) findViewById(R.id.tv_nickname);
        this.etphone = (EditText) findViewById(R.id.et_phone);
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.add_contract));

        iv_pic = (ImageView) findViewById(R.id.iv_pic);
        tv_add = (TextView) findViewById(R.id.tv_add);

        tv_add.setOnClickListener(this);
        iv_left.setOnClickListener(this);

        //搜索
        etphone.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {


                    //防止连续两次触发该事件
                    long currentTimeMillis = System.currentTimeMillis();
                    if (currentTimeMillis - lastTime < 1000) {
                        return false;
                    }
                    lastTime  = currentTimeMillis;

                    if (TextUtils.isEmpty(etphone.getText().toString().trim())
                            || !CommonUtils.isPhone(etphone.getText().toString().trim())) {

                        showToast("请输入正确的手机号码");

                        setNull();//数据置为初始值

                        return true;
                    }

                    if (sp.getString(Constant.USERID, "").equals(etphone.getText().toString().trim())) {
                        //输入的是自己的手机号码
                        showToast("您不能添加自己为好友");

                        setNull();//数据置为初始值
                        return true;
                    }

                    JSONObject j = new JSONObject();
                    j.put("token", sp.getString(Constant.TOKEN, ""));
                    j.put("phoneNum", etphone.getText().toString().trim());

                    HttpUtil.setPostRequest("搜索中...", SearchContractActivity.this, Constant.WEB_ROOT_TALK + Constant.SEARCHUSER,
                            j.toString(), getClass().getName(), new HttpUtil.SuccessListener() {
                                @Override
                                public void setOnSuccessListener(String str) {

                                    try {

                                        SearchContract sc = JSONObject.parseObject(str, SearchContract.class);

                                        if (sc.code == Constant.STATUS_SUCCESS) {

                                            //判断是否是好友
                                            isFriend(sc);

                                            if (sc.userInfo != null) {

                                                Glide.with(SearchContractActivity.this)
                                                        .load(sc.userInfo.imgUrl)
                                                        .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                                                        .placeholder(R.mipmap.temp_pic).into(iv_pic);

                                                tvnickname.setText(sc.userInfo.nickName);
                                                tvtel.setText(sc.userInfo.phoneNum);

                                                invitePhoneNum = sc.userInfo.phoneNum;
                                                tv_add.setVisibility(View.VISIBLE);

                                            } else {

                                                showToast("未查询到信息");

                                                setNull();
                                            }

                                        } else {
                                            setNull();

                                            showToast(Constant.showErrorMsg(sc.code, SearchContractActivity.this));
                                        }

                                    } catch (Exception e) {
                                        e.printStackTrace();

                                        showToast("未查询到信息");
                                        setNull();
                                    }

                                }
                            }, new HttpUtil.FailListener() {
                                @Override
                                public void setOnFailListener() {
                                    setNull();

                                }
                            });

                    return true;
                }
                return false;

            }
        });

    }


    private void isFriend(SearchContract sc) {
        if (sc.isFriend) {//已经是好友
            tv_add.setClickable(false);
            tv_add.setEnabled(false);
            tv_add.setFocusable(false);
            tv_add.setText(getResources().getString(R.string.alreday_contract));
        }else {
            tv_add.setClickable(true);
            tv_add.setEnabled(true);
            tv_add.setFocusable(true);
            tv_add.setText(getResources().getString(R.string.add_contract));
        }
    }

    private void setNull() {

        tvnickname.setText("");
        tvtel.setText("");
        iv_pic.setImageBitmap(null);
        invitePhoneNum = "";

        tv_add.setVisibility(View.INVISIBLE);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.tv_add: //添加好友

                if (!CommonUtils.isNull(invitePhoneNum)) {

                    Intent mIntent = new Intent(this, InviteContractActivity.class);
                    mIntent.putExtra("invitePhoneNum", invitePhoneNum);
                    startActivity(mIntent);

                }

                break;

            case R.id.iv_left:
                finish();
                break;


        }

    }
}
